package com.ruoyi.business.controller;

import com.ruoyi.business.service.ITRegisterNoticeService;
import com.ruoyi.business.vo.TRegisterNoticeVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.business.vo.TRegisterLeaveVo;
import com.ruoyi.business.service.ITRegisterLeaveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 请假记录Controller
 *
 * @author Tellsea
 * @date 2024-02-06
 */
@Api(tags = "请假记录Controller")
@RestController
@RequestMapping("/business/registerLeave")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TRegisterLeaveController extends BaseController {

    private final ITRegisterLeaveService tRegisterLeaveService;

    private final ITRegisterNoticeService itRegisterNoticeService;


    @ApiOperation("查询请假记录列表")
    @PreAuthorize("@ss.hasPermi('business:registerLeave:list')")
    @GetMapping("/list")
    public TableDataInfo<TRegisterLeaveVo> list(TRegisterLeaveVo entity) {
        return tRegisterLeaveService.queryList(entity);
    }

    @ApiOperation("查询请假记录所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TRegisterLeaveVo entity) {
        return AjaxResult.success("查询成功", tRegisterLeaveService.queryAll(entity));
    }

    @ApiOperation("导出请假记录列表")
    @PreAuthorize("@ss.hasPermi('business:registerLeave:export')")
    @Log(title = "请假记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TRegisterLeaveVo entity) {
        List<TRegisterLeaveVo> list = tRegisterLeaveService.queryAll(entity);
        ExcelUtil<TRegisterLeaveVo> util = new ExcelUtil<>(TRegisterLeaveVo.class);
        util.exportExcel(response, list, "请假记录数据");
    }

    @ApiOperation("获取请假记录详细信息")
    @PreAuthorize("@ss.hasPermi('business:registerLeave:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tRegisterLeaveService.queryById(id));
    }

    @ApiOperation("新增请假记录")
    @PreAuthorize("@ss.hasPermi('business:registerLeave:add')")
    @Log(title = "请假记录", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody @Validated TRegisterLeaveVo entity) {
        TRegisterNoticeVo tRegisterNoticeVo = itRegisterNoticeService.queryByRegisterId(entity.getRegisterId());
        if(entity.getLeaveStartDate().compareTo(tRegisterNoticeVo.getEndDate())<0
                || entity.getLeaveEndDate().compareTo(tRegisterNoticeVo.getEndDate())<0){
            return AjaxResult.error("请假时间必须要当前进修时间范围内");
        }
        boolean save = tRegisterLeaveService.save(entity);
        itRegisterNoticeService.overLeaveDurationHandle();
        itRegisterNoticeService.revertOverLeaveDurationHandle();
        return toAjax(save);
    }

    @ApiOperation("修改请假记录")
    @PreAuthorize("@ss.hasPermi('business:registerLeave:edit')")
    @Log(title = "请假记录", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TRegisterLeaveVo entity) {
        boolean updateById = tRegisterLeaveService.updateById(entity);
        itRegisterNoticeService.overLeaveDurationHandle();
        itRegisterNoticeService.revertOverLeaveDurationHandle();
        return toAjax(updateById);
    }

    @ApiOperation("删除请假记录")
    @PreAuthorize("@ss.hasPermi('business:registerLeave:remove')")
    @Log(title = "请假记录", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tRegisterLeaveService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
